﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CMU.Certificates;

namespace CMU.Manage
{
    public partial class UCChungChi : UserControl
    {
        CertificateEntities context = new CertificateEntities();
        public UCChungChi()
        {
            InitializeComponent();
        }

        // LOAD
        private void UCChungChi_Load(object sender, EventArgs e)
        {
            var loadcc = from cc in context.ChungChi
                         select cc;
            dataGridView1.DataSource = loadcc.ToList();
            dataGridView1.Refresh();

            var sinhvien = from sv in context.SinhVien
                           select sv;
            comboBox1.DataSource = sinhvien.ToList<SinhVien>();
            comboBox1.DisplayMember = "MaSV";
            comboBox1.ValueMember = "Id";
            //var lophoc = from lh in context.LopHoc
            //               select lh;
            //comboBox2.DataSource = lophoc.ToList<LopHoc>();
            //comboBox2.DisplayMember = "NamHoc";
            //comboBox3.DataSource = lophoc.ToList<LopHoc>();
            //comboBox3.DisplayMember = "HocKy";
            //comboBox2.ValueMember = "Id";
            //comboBox1.DataSource = lophoc.ToList<LopHoc>();
            //comboBox1.DisplayMember = "TenMH";
            //comboBox1.ValueMember = "Id";
        }

        // ADD
        private void button2_Click(object sender, EventArgs e)
        {

            ChungChi cc = new ChungChi();
            cc.Diem = double.Parse(textBox2.Text);

            context.ChungChi.Add(cc);
            int count = context.SaveChanges();
            if (count > 0)
            {
                MessageBox.Show("Da them moi");
                UCChungChi_Load(sender, e);
            }
            else
                MessageBox.Show("Da ton tai");
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button6_Click(object sender, EventArgs e)
        {
            int ccID;
            if (Int32.TryParse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString(), out ccID))
            {
                using (CertificateEntities newcontext = new CertificateEntities())
                {
                    ChungChi cc = new ChungChi() { Id = ccID };
                    newcontext.ChungChi.Attach(cc);
                    //context.ObjectStateManger.ChangeObjectState(mh, EntityState.Deleted);
                    //newcontext.Entry(mh).State = System.Data.EntityState.Deleted;
                    newcontext.ChungChi.Remove(cc);

                    int count = newcontext.SaveChanges();
                    if (count > 0)
                    {
                        MessageBox.Show("Da xoa");
                        UCChungChi_Load(sender, e);
                    }
                    else
                    {
                        MessageBox.Show("Mon Hoc da duoc lien ket");
                    }

                }
            }
        }


 
    }
}
